package com.pig4cloud.pig.resident.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.resident.feign.dto.ResidentDto;
import com.pig4cloud.pig.resident.feign.dto.UnResidentDto;
import com.pig4cloud.pig.resident.feign.entity.Resident;
import com.pig4cloud.pig.resident.service.ResidentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/resident" )
@Tag(description = "resident" , name = "resident 表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ResidentController {
	@Autowired
	private final ResidentService residentService;
	@Operation(summary = "新增resident 表" , description = "新增resident 表" )
	@SysLog("新增住户表" )
	@PostMapping("/addResident")
	public R save(@Valid @RequestBody Resident resident) {
		Resident resident1 = residentService.checkPhone(resident);
		if (resident1 != null) {
			return R.failed("该手机号已存在");
		}
		return R.ok(residentService.save(resident));
	}

	@DeleteMapping("/deleteResident")
	@SysLog("删除住户表" )
	public R deleteResident(@RequestParam Long[] ids) {
//		UpdateWrapper<Resident> updateWrapper = new UpdateWrapper<>();
//		updateWrapper.in("resident_id", Arrays.asList(ids));
//		updateWrapper.set("del_flag", "1");
//		return R.ok(residentService.update(updateWrapper));
		return R.ok(residentService.removeByIds(Arrays.asList(ids)));
	}

	@PutMapping("/updateResident")
	public R updateResident(@Valid @RequestBody Resident resident) {
		Resident resident1 = residentService.checkPhone(resident);
		System.out.println("resident1 = " + resident1+ ", resident = " + resident);
		if (resident1 != null && !resident1.getResidentId().equals(resident.getResidentId())) {
			return R.failed("该手机号已存在");
		}
		return R.ok(residentService.updateById(resident));
	}
	@GetMapping("/page")
	public R getUserPage(@ParameterObject Page page, @ParameterObject Resident resident) {
		QueryWrapper<Resident> wrapper = new QueryWrapper<>();
		wrapper.like(StrUtil.isNotEmpty(resident.getResidentName()), "resident_name", resident.getResidentName());
		wrapper.like(StrUtil.isNotEmpty(resident.getPhone()), "phone", resident.getPhone());
		wrapper.orderByDesc("create_time");
		return R.ok(residentService.page(page, wrapper));
	}

	@GetMapping("/getResidentById")
	public R getResidentById(@RequestParam Long residentId) {
		return R.ok(residentService.getById(residentId));
	}

	@GetMapping("/getResident")
	public R getResident(@RequestParam Long roomId) {
		List<Resident> residents = residentService.getResident(roomId);
		return R.ok(residents);
	}

	@PostMapping("/getUnResident")
	public Page<Resident> getUnResident(@RequestBody UnResidentDto unResidentDto) {
		QueryWrapper<Resident> wrapper = new QueryWrapper<>();
		wrapper.like(StrUtil.isNotEmpty(unResidentDto.getResidentName()), "resident_name", unResidentDto.getResidentName());
		wrapper.like(StrUtil.isNotEmpty(unResidentDto.getPhone()), "phone", unResidentDto.getPhone());
		String[] phones = residentService.getResidentPhone(unResidentDto.getRoomId());
		wrapper.notIn(phones != null && phones.length > 0, "phone", phones);
		Page<Resident> page = new Page<>(unResidentDto.getCurrent(), unResidentDto.getSize());
		return residentService.page(page, wrapper);
	}

	@DeleteMapping("/deleteRelativeResident")
	public R deleteRelativeResident(@RequestParam Long[] ids, @RequestParam Long roomId) {
		residentService.deleteResident(ids, roomId);
		return R.ok();
	}

	@GetMapping("/addRelativeResident")
	public R addRelativeResident(@RequestParam Long[] ids, @RequestParam Long roomId) {
		residentService.addRelativeResident(ids, roomId);
		return R.ok();
	}



}
